package com.miniprogram.common.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miniprogram.bean.po.generic.DataEntity;
import lombok.extern.slf4j.Slf4j;

/**
 * BaseServiceImpl
 *
 * @author lin
 * @since 2018年11月23日
 */
@Slf4j
public class BaseServiceImpl<M extends BaseMapper<T>, T extends DataEntity> extends ServiceImpl<M, T> implements BaseService<T> {

    @Override
    public boolean save(T t) {
        if (null == t) {
            return false;
        }
        t.setCreateTime(new DateTime());
        t.setUpdateTime(new DateTime());
        return this.baseMapper.insert(t) > 0;
    }

    @Override
    public boolean updateById(T t) {
        if (null == t) {
            return false;
        }
        t.setUpdateTime(new DateTime());
        return this.baseMapper.updateById(t) > 0;
    }

    @Override
    public boolean saveOrUpdate(T t) {
        if (null == t) {
            return false;
        }
        if (ObjectUtil.isNull(t)) {
            return this.save(t);
        } else {
            return this.updateById(t);
        }
    }

//    @Override
//    public Boolean logicDelete(Integer id) {
//        Class<T> clazz = super.currentModelClass();
//        T t = null;
//        try {
//            t = clazz.newInstance();
//        } catch (Exception e) {
//            log.error("logicDelete Failed!", e);
//            Shift.fatal(RestStatusEnum.ERROR);
//        }
//        t.setId(id);
//        t.setLogicDelete(true);
//        return baseMapper.updateById(t) > 0 ? true : false;
//    }
}
